package top.zuli.package02.homework14;

import java.util.concurrent.locks.ReentrantLock;

public class AccountTest1 {
    public static void main(String[] args) {
        Runnable runnable = new Account1();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.start();
        t2.start();
    }
}
class Account1 implements Runnable{
    private int money = 0;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        for (int i = 1; i <=3; i++) {
            try {
                lock.lock();
                if(money < 3000){
                    try {
                        Thread.sleep(250);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    money += 1000;
                    System.out.println(Thread.currentThread().getName()+"：存钱成功。余额为："+money);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}

